﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace POC
{

    public partial class Form1 : Form
    {
        
        public Form1()
        {

            this.Text = "Address Book";
            InitializeComponent();
            try
            {
                
                // Automatically generate the DataGridView columns.
                table.AutoGenerateColumns = true;

                // Set up the data source.

                table.DataSource = AddressEntryDAO.LoadAll();

                // Automatically resize the visible rows.
                table.AutoSizeRowsMode =
                    DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;


                // Set property values appropriate for read-only display and  
                // limited interactivity. 
                table.ReadOnly = true;
                table.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                table.MultiSelect = false;

                table.RowHeadersWidthSizeMode =
                  DataGridViewRowHeadersWidthSizeMode.DisableResizing;


            }
            catch (SqlException)
            {
                MessageBox.Show("There was a problem connecting to the database.", "ERROR",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                System.Threading.Thread.CurrentThread.Abort();
            }
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {

        }


        private void table_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Globals.lastClicked = new AddressEntry(table["FirstName", e.RowIndex].Value.ToString(),table["LastName", e.RowIndex].Value.ToString(), table["Email", e.RowIndex].Value.ToString());
        }

        private void Createbtn_Click(object sender, EventArgs e)
        {
            Globals.Create = true;
            Form2 form2 = new Form2();
            form2.FormClosed += new FormClosedEventHandler(CloseForm);
            form2.Show();
        }

        private void Updatebtn_Click(object sender, EventArgs e)
        {
            Globals.Create = false;
            Form2 form2 = new Form2();
            form2.FormClosed +=new FormClosedEventHandler(CloseForm);
            form2.Show();
            //table.DataSource = AddressEntryDAO.LoadAll();
        }

        private void Deletebtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to delete this entry?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // a 'DialogResult.Yes' value was returned from the MessageBox 
                // proceed with your deletion
                AddressEntryDAO.Delete(Globals.lastClicked);
                //table.Rows.Clear();
                table.DataSource = AddressEntryDAO.LoadAll();
            } 
            
        }
        private void CloseForm(object sender, EventArgs e)
        {
            table.DataSource = AddressEntryDAO.LoadAll();
        }

        private void toolStripDropDownButton3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Please click on a row and use one of the buttons on the bottom of the screen or in the file menu.", "Help",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            System.Threading.Thread.CurrentThread.Abort();
        }
    }
}
